#include "visionmonitorwidget.h"

VisionMonitorWidget::VisionMonitorWidget(QWidget *parent) : QWidget(parent)
{
    setUI();

    connect(btns_monitor, SIGNAL(buttonClicked(int)), this, SLOT(btns_monitor_clicked(int)));
}

void VisionMonitorWidget::setUI()
{
    QHBoxLayout *hbox = new QHBoxLayout;

    /* --------------左侧识别与控制区 frame_panel---------------- */
    //实时回传
    QGroupBox *frame_panel = new QGroupBox;
    frame_panel->setTitle("回显区:");
    QVBoxLayout *vbox_echo = new QVBoxLayout;
    QFrame *k210_echo = new QFrame;
    QVBoxLayout *vbox_cam = new QVBoxLayout;
    cam = new QCamera(this);
    view_finder = new QCameraViewfinder;
    cam->setViewfinder(view_finder);
    vbox_cam->addWidget(view_finder);
    k210_echo->setLayout(vbox_cam);

    QHBoxLayout *echo_func = new QHBoxLayout;
    QLabel *seek_text = new QLabel("巡线模式:");
    seek_status = new QLabel;
    seek_status->setText("开");
    QPushButton *btn_autoseek = new QPushButton("开启巡线");
    QPushButton *btn_stopseek = new QPushButton("暂停巡线");
    QPushButton *btn_capture = new QPushButton("捕获图像");
    QPushButton *btn_identify = new QPushButton("进行识别");
    echo_func->addWidget(seek_text);
    echo_func->addWidget(seek_status);
    echo_func->addWidget(btn_autoseek);
    echo_func->addWidget(btn_stopseek);
    echo_func->addWidget(btn_capture);
    echo_func->addWidget(btn_identify);
    btns_monitor = new QButtonGroup;
    btns_monitor->addButton(btn_autoseek, 0);
    btns_monitor->addButton(btn_stopseek, 1);
    btns_monitor->addButton(btn_capture, 2);
    btns_monitor->addButton(btn_identify, 3);

    vbox_echo->addWidget(k210_echo);
    vbox_echo->addLayout(echo_func);
    frame_panel->setLayout(vbox_echo);
    frame_panel->setFixedSize(450, 430);

    /* ----------------右侧结果显示区 vbox_result----------------- */
    QVBoxLayout *vbox_result = new QVBoxLayout;

    //历史捕获
    QGroupBox *frame_hist = new QGroupBox;
    frame_hist->setTitle("历史捕获区:");
    QHBoxLayout *hbox_hist = new QHBoxLayout;
    QLabel *lbc = new QLabel("捕获图像");
    lbc->setMaximumHeight(100);
    img_capcture = new QCameraImageCapture(cam);
    connect(img_capcture, &QCameraImageCapture::imageSaved, this, [=]() {
        lbc->setPixmap(QPixmap("D:/QT/tmp.jpg").scaled(160, 80, Qt::KeepAspectRatio));
    });
    hbox_hist->addWidget(lbc);
    frame_hist->setLayout(hbox_hist);
    frame_hist->setFixedHeight(100);

    //识别列表
    QGroupBox *frame_result = new QGroupBox;
    frame_result->setTitle("识别结果:");
    QVBoxLayout *vbox_res = new QVBoxLayout;
    MyTableView *table_fruits = new MyTableView;
    Fruit f1 = { "苹果", QColor(255, 255, 255), QPoint(2, 3), 2 };
    table_fruits->addItemLine(f1);
    vbox_res->addWidget(table_fruits);
    frame_result->setLayout(vbox_res);

    vbox_result->addWidget(frame_hist);
    vbox_result->addWidget(frame_result);

    hbox->addWidget(frame_panel);
    hbox->addLayout(vbox_result);
    this->setLayout(hbox);
}

void VisionMonitorWidget::btns_monitor_clicked(int id)
{
    switch (id) {
    case 0: //开启巡线
        seek_status->setText("开");

        break;
    case 1: //停止巡线
        seek_status->setText("关");

        break;
    case 2: //捕获图像
        img_capcture->capture("D:/QT/tmp.jpg");

        break;
    case 3: //进行识别
        view_finder->show();
        cam->start();

        break;
    default:
        break;
    }
}
